﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Code4WP7SDK;
using LuWeibo.Models;
using LuWeiboSDK;
using System.Collections.Generic;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace LuWeibo.ViewModels
{
    public class StatusDetailViewModel : BaseModel
    {
        #region Fields
        private Status _currentStatus; //当前展示的微博 
        private ObservableCollection<Comments> _currentComments;//当前微博的评论列表
        private bool _listVisible; //列表是否显示 -- 这个地方可能会涉及到显示的性能问题
        private bool _noCommentsVisible;//没有评论的标记
        #endregion

        #region Command
        
        #endregion

        #region Properteis
        public Status CurrentStatus
        {
            get
            {
                if (DesignerProperties.IsInDesignTool)
                {
                    this._currentStatus = new Status
                        {
                            created_at = "Thu Mar 29 10:13:06 +0800 2012",
                            id = 3428859137993491,
                            mid = "3428859137993491",
                            idstr = "3428859137993491",
                            text = "好爸爸十条标准，你是好爸爸吗？[可爱]",
                            source = "<a href=\"http://weibo.com\" rel=\"nofollow\">新浪微博</a>",
                            favorited = false,
                            truncated = false,
                            in_reply_to_status_id = "",
                            in_reply_to_user_id = "",
                            in_reply_to_screen_name = "",
                            geo = null,
                            IsShareVisible = true,
                            user = new User
                            {
                                id = "1713926427",
                                idstr = "1713926427",
                                screen_name = "微博搞笑排行榜",
                                name = "微博搞笑排行榜",
                                province = "44",
                                city = "1",
                                location = "广东 广州",
                                description = "微博搞笑中心！每天搜罗最搞笑最好玩的微博。关注我，获得每日新鲜笑料！（欢迎合作，投稿）↖(^ω^)↗",
                                url = "http://weibo.com/mkdqs",
                                profile_image_url = "http://tp4.sinaimg.cn/1713926427/50/5615669283/0",
                                profile_url = "topgirls8",
                                domain = "topgirls8",
                                weihao = "",
                                gender = "f",
                                followers_count = 7199354,
                                friends_count = 915,
                                statuses_count = 36517,
                                favourites_count = 4437,
                                created_at = "Fri Mar 19 00:00:00 +0800 2010",
                                following = true,
                                allow_all_act_msg = true,
                                geo_enabled = false,
                                verified = false,
                                remark = "",
                                allow_all_comment = true,
                                avatar_large = "http://tp4.sinaimg.cn/1713926427/180/5615669283/0",
                                verified_reason = "",
                                follow_me = false,
                                online_status = 1,
                                bi_followers_count = 278,
                            },
                            retweeted_status = new Status
                            {
                                created_at = "Wed Mar 28 22:12:14 +0800 2012",
                                id = 3428677721732850,
                                mid = "3428677721732850",
                                idstr = "3428677721732850",
                                text = "【好爸爸十条标准】1.下班就回家。2.到家先陪孩子玩。3.不训斥，不打孩子。4.不当着孩子面吵架。5.不当着孩子面抽烟。6.不当着孩子面上网玩游戏。7.当孩子面对帮你的家人说“谢谢”。8.当孩子面洗自己的臭袜子。9.当着孩子面把碗里的饭菜吃干净。10.当全家人的面夸奖做对事的孩子。爸爸们加油！！",
                                source = "<a href=\"http://weibo.com\" rel=\"nofollow\">新浪微博</a>",
                                favorited = false,
                                truncated = false,
                                in_reply_to_status_id = "",
                                in_reply_to_user_id = "",
                                in_reply_to_screen_name = "",
                                thumbnail_pic = "http://ww2.sinaimg.cn/thumbnail/6a1f0209gw1drfvb2y0kvj.jpg",
                                bmiddle_pic = "http://ww2.sinaimg.cn/bmiddle/6a1f0209gw1drfvb2y0kvj.jpg",
                                original_pic = "http://ww2.sinaimg.cn/large/6a1f0209gw1drfvb2y0kvj.jpg",
                                geo = null,
                                user = new User
                                {
                                    id = "1780417033",
                                    idstr = "1780417033",
                                    screen_name = "实用小百科",
                                    name = "实用小百科",
                                    province = "44",
                                    city = "1",
                                    location = "广东 广州",
                                    description = "实用小百科，专门搜集生活中的实用小常识、小智慧。为您的生活提供更多帮助与便利。",
                                    url = "",
                                    profile_image_url = "http=//tp2.sinaimg.cn/1780417033/50/1280367872/0",
                                    profile_url = "xfxzh",
                                    domain = "xfxzh",
                                    weihao = "",
                                    gender = "f",
                                    followers_count = 2910068,
                                    friends_count = 476,
                                    statuses_count = 13670,
                                    favourites_count = 500,
                                    created_at = "Wed Jul 21 00=00=00 +0800 2010",
                                    following = true,
                                    allow_all_act_msg = true,
                                    geo_enabled = true,
                                    verified = false,
                                    remark = "",
                                    allow_all_comment = true,
                                    avatar_large = "http=//tp2.sinaimg.cn/1780417033/180/1280367872/0",
                                    verified_reason = "",
                                    follow_me = false,
                                    online_status = 0,
                                    bi_followers_count = 119,
                                }
                            }
                        };
                }
                return _currentStatus;
            }
            set
            {
                _currentStatus = value;
                this.NotifyPropertyChanged(p => p.CurrentStatus);
            }
        }

        public ObservableCollection<Comments> CurrentComments
        {
            get
            {
                if (this._currentComments == null)
                {
                    _currentComments = new ObservableCollection<Comments>();
                }
                return _currentComments;
            }
            set
            {
                _currentComments = value;
                this.NotifyPropertyChanged(p => p.CurrentComments);
            }
        }

        public bool ListVisible
        {
            get { return _listVisible; }
            set
            {
                _listVisible = value;
                this.NotifyPropertyChanged(p => p.ListVisible);
            }
        }

        public bool NoCommentsVisible
        {
            get { return _noCommentsVisible; }
            set
            {
                _noCommentsVisible = value;
                this.NotifyPropertyChanged(p => p.NoCommentsVisible);
            }
        }
        #endregion

        #region Method
        /// <summary>
        /// 获取评论列表内容
        /// </summary>
        /// <param name="statusId"></param>
        public void GetCommentsExecute(Int64 statusId)
        {
            List<Parameter> paras = new List<Parameter>();
            paras.Add(new Parameter("id", CurrentStatus.id));
            string data = ParameterHelper.BuildParameters(paras);
            WebClientService ws = new WebClientService();
            ws.WebClientCompleted += (s, e) =>
                {
                    if (e.Ex == null)
                    {
                        Debug.WriteLine(e.Result.ToString());
                        using (JsonHelper jhelper = new JsonHelper(e.Result.ToString()))
                        {
                            this.CurrentComments = jhelper.get<ObservableCollection<Comments>>("comments");
                            if (this.CurrentComments.Count == 0)
                            {
                                this.ListVisible = false;
                                this.NoCommentsVisible = true;
                            }
                            else
                            {
                                this.ListVisible = true;
                                this.NoCommentsVisible = false;
                            }
                        }
                    }
                    else
                    {
                        //获取当前微博的评论列表失败
                    }
                };
            ws.GetRequest(Urls.comments_show, data);
        }
         #endregion

    }
}
